package com.team.service.impl;

import com.team.dao.IndexTitleMapper;
import com.team.dao.NewsMapper;
import com.team.dao.NoticeMapper;
import com.team.entity.IndexTitle;
import com.team.entity.News;
import com.team.entity.Notice;
import com.team.service.IndexTitleService;
import com.team.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * -*- coding:utf-8 -*-
 *
 * @author 太阳偷渡青山
 * @software IntelliJ IDEA
 * @date 2022/10/18 8:00
 * @Description
 */
@Service
public class IndexTitleServiceImpl implements IndexTitleService {
    @Autowired
    private IndexTitleMapper indexTitleMapper;
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public Result findAll() {
        List<IndexTitle> indexTitles = indexTitleMapper.selectAll();
        List<IndexTitle> indexTitleList = hierarchy(indexTitles);

        //查询公告
        List<Notice> notices = noticeMapper.selectAllByUppid();

        HashMap<Object, Object> map = new HashMap<>();
        map.put("menu",indexTitleList);
        map.put("notices",notices);

        return new Result(200,"查询成功",map);
    }

    @Override
    public Result findById(Integer id) {
        IndexTitle title = indexTitleMapper.selectByPrimaryKey(id);
        return new Result(200,"查询成功",title);
    }

    private List<IndexTitle> hierarchy(List<IndexTitle> indexTitleList){

        //1.一级菜单
        List<IndexTitle> firsts=new ArrayList<>();
        for (IndexTitle indexTitle:indexTitleList){
            if(indexTitle.getParentId()==0){
                firsts.add(indexTitle);
            }
        }

        //2.查询一级菜单下的二级菜单
        for(IndexTitle first:firsts){
            List<IndexTitle> children=new ArrayList<>();
            for(IndexTitle p:indexTitleList){
                if(first.getId() == p.getParentId()){
                    children.add(p);
                }
            }
            first.setChildren(children);
        }

        return firsts;
    }

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public Result findAllKind1() {
        List<News> allKind1 = newsMapper.findAllKind1();
        return new Result(200,"查询新闻",allKind1);
    }

    @Override
    public Result findAllKind1ById(Integer id) {
        News allKind1ById = newsMapper.findAllKind1ById(id);
        return new Result(200,"查询成功",allKind1ById);
    }

    @Override
    public Result findAllKind2() {
        List<News> allKind1 = newsMapper.findAllKind2();
        return new Result(200,"查询新闻",allKind1);
    }

    @Override
    public Result findAllKind2ById(Integer id) {
        News allKind1ById = newsMapper.findAllKind2ById(id);
        return new Result(200,"查询成功",allKind1ById);
    }


}
